import algos.*;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

import utils.Pair;

import static utils.Common.*;

public class Draft {

    private static List<String> getWords(String line) {
        StringTokenizer tokenizer = new StringTokenizer(line);
        List<String> result = new ArrayList<String>(tokenizer.countTokens());
        while (tokenizer.hasMoreTokens()) {
            result.add(tokenizer.nextToken());
        }
        return result;
    }

    public static void main(String[] args) throws IOException {
        System.out.println("Completion project (c) 2011");

        List<Pair<String, Double>> ngf = newArrayList();
        ngf.add(makePair("1grams/out", 0.000000000001));
        ngf.add(makePair("2grams/out", 0.0000001));
        ngf.add(makePair("3grams/out", 0.999999899999));

        long time1, time2;
        time1 = System.currentTimeMillis();
        //Engine engine = Engine.create(ngf);
        time2 = System.currentTimeMillis();


        List<String> ngf2 = newArrayList();
        ngf2.add("1grams/out");
        ngf2.add("2grams/out");
        ngf2.add("3grams/out");
        UserEngine engine = UserEngine.create(ngf2);
        List<Double> coreCoefs = newArrayList();
        List<Double> userCoefs = newArrayList();
        coreCoefs.add(0.000000000001);
        coreCoefs.add(0.0000001);
        coreCoefs.add(0.199999899999);

        userCoefs.add(0.1);
        userCoefs.add(0.1);
        userCoefs.add(0.6);
        engine.setCoefs(coreCoefs, userCoefs);

        //IVocabulary vocabulary = engine.getVocabulary();
        //       System.out.println("models are created: " + (time2 - time1) + "ms");
        //  System.out.println("vocabulary size: " + vocabulary.size());
        System.out.println("input empty string to quit");

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            System.out.print("> ");
            String line = reader.readLine();
            if (line == null || line.trim().length() == 0) {
                break;
            }

            String prefix = "";
            if (Character.isLetter(line.charAt(line.length() - 1))) {
                int x = line.lastIndexOf(' ');
                if (x < 0) {
                    prefix = line;
                    line = "";
                } else {
                    prefix = line.substring(x + 1);
                    line = line.substring(0, x);
                }
            }

            time1 = System.currentTimeMillis();
            List<String> l = getWords(line);
            //List<Pair<Double, String>> answer = engine.get(l, prefix/*, 0*/);
            List<Pair<Double, String>> answer = engine.get(l, prefix, 0);
            time2 = System.currentTimeMillis();

            int maxLen = 0;
            for (Pair<Double, String> p : answer) {
                maxLen = Math.max(maxLen, (l + " " + p.second).length());
            }
            for (Pair<Double, String> p : answer) {
                String text = l + " " + p.second;
                while (text.length() < maxLen) text += ' ';
                System.out.println(text + "  " + String.format("%.18f", p.first));
            }

            System.out.println(time2 - time1 + "ms");
        }
        System.out.println();
    }

}
